package Com.Hupochuan.Dal.Tab;

import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import Com.Hupochuan.Dal.DBManager.DBManager;
import Com.Hupochuan.Vo.Actor;
import Com.Hupochuan.Vo.Year;

public class ActorTab {
	/*
	 * 本类私有对象
	 */
	private DBManager<Actor> dbManager = new DBManager<Actor>(Actor.class);
	
	/*
	 * 单一实例
	 */
	private ActorTab(){}
	private static ActorTab atorTab = new ActorTab();
	public static ActorTab getObject(){
		return atorTab;
	}
	
	/*
	 * 获得演员对象
	 */
	public Actor findActor(long id){
		//参数部分
		Actor actor = null;
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select * from Actor where id=${id} ");
		
		//sql参数部分
		paraHash.put("id",id);
		
		//从列表中获得对象
		actor = dbManager.querySqlReturnVo(sqlBuff.toString(),paraHash);
		
		//返回
		return actor;
	}
	
	/*
	 * 演员列表 前10条
	 */
	public List<Actor> findActorListTop10(Year year,long areaSortId,long typeId,long directorId){
		
		//参数部分
		List<Actor> actorList = new Vector<Actor>();
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select distinct Actor.id,Actor.name from Actor ");
		sqlBuff.append(" left join MovieActor on MovieActor.actorId=Actor.id ");
		sqlBuff.append(" left join Movie on Movie.id=MovieActor.movieId ");
		sqlBuff.append(" where 1=1 ");
		
		//选择了年份
		if(year != null){
			sqlBuff.append(" 	and Movie.releaseDate>=${startDate} ");
			sqlBuff.append(" 	and Movie.releaseDate<=${endDate} ");
			
			//sql参数部分
			paraHash.put("startDate",year.getStartDate());
			paraHash.put("endDate",year.getEndDate());
		}
		
		//如果选择了地区分类
		if(areaSortId != 0){
			sqlBuff.append(" and Movie.areaId in ( ");
			sqlBuff.append(" 	select id from Area ");
			sqlBuff.append(" 	where Area.sortId=${areaSortId} ");
			sqlBuff.append(" ) ");
		}
		
		//如果选择了类型
		if(typeId != 0){
			sqlBuff.append(" and Movie.id in ( ");
			sqlBuff.append(" 	select movieId from MovieType ");
			sqlBuff.append(" 	where MovieType.typeId=${typeId} ");
			sqlBuff.append(" ) ");
		}
		
		//如果选择了导演
		if(directorId != 0){
			sqlBuff.append(" and Movie.directorId=${directorId} ");
		}
		
		sqlBuff.append(" order by Actor.weight desc,Actor.id limit 0,10 ");
		
		//sql参数部分
		paraHash.put("areaSortId",areaSortId);
		paraHash.put("typeId",typeId);
		paraHash.put("directorId",directorId);
		
		//执行sql 
		actorList = this.dbManager.querySqlReturnVoList(sqlBuff.toString(),paraHash);
		
		//返回列表
		return actorList;
	}
}
